﻿using System;
using System.Reflection;
using System.Diagnostics;

namespace BellAssistant.Common.CommonHelper
{
    /// <summary>
    /// 文件版本帮助类
    /// </summary>
    public class VersionHelper
    {
        #region 程序集特性访问器
        public string AssemblyTitle
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
                if (attributes.Length > 0)
                {
                    AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
                    if (titleAttribute.Title != "")
                    {
                        return titleAttribute.Title;
                    }
                }
                return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
            }
        }

        public string AssemblyVersion
        {
            get
            {
                return Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
        }

        public string AssemblyDescription
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyDescriptionAttribute)attributes[0]).Description;
            }
        }

        public string AssemblyProduct
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyProductAttribute)attributes[0]).Product;
            }
        }

        public string AssemblyCopyright
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
            }
        }

        public string AssemblyCompany
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCompanyAttribute)attributes[0]).Company;
            }
        }

        private static string fileVersion = string.Empty;
        /// <summary>
        /// 文件版本
        /// </summary>
        //public static string AssemblyFileVersion
        //{
        //    get
        //    {
        //        fileVersion = FileVersionInfo.GetVersionInfo(Application.ExecutablePath).FileVersion;
        //        return fileVersion;
        //    }
        //    set
        //    {
        //       fileVersion = value;
        //    }
        //}
        #endregion

        #region 获取新版本号
        /// <summary>
        /// 根据当前的版本号创建下一个版本号
        /// </summary>
        /// <param name="rvc">RegionVersionCode：当前地区版本代号枚举值</param>
        /// <returns>新版本号</returns>
        //public static string SetNewVersion(RegionVersionCode rvc)
        //{
        //    string version = AssemblyFileVersion;
        //    if (!string.IsNullOrEmpty(version))
        //    {
        //        string[] vers = version.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
        //        if (vers != null && vers.Length == 4)
        //        {
        //            string oldMajor = vers[0];//主版本
        //            string oldMinor = vers[1];//次版本
        //            string oldBuild = vers[2];//内部版本号
        //            string oldRevision = vers[3];//修订号

        //            DateTime dtNow = DateTime.Now.ToUniversalTime();
        //            string major = dtNow.Year.ToString().Substring(2, 2);
        //            string minor = dtNow.Month.ToString().PadLeft(2, '0');
                    
        //            int code = (int)Enum.Parse(typeof(RegionVersionCode), rvc.ToString()); //获取地区对应的代码
        //            string regionCode = code.ToString();
        //            if (regionCode.Length <= 2)
        //            {
        //                regionCode = regionCode.PadLeft(2, '0');
        //            }
        //            else
        //            {
        //                if ((regionCode.Length % 2) == 0)
        //                {
        //                    regionCode = regionCode.PadLeft(regionCode.Length, '0');
        //                }
        //                else
        //                {
        //                    regionCode = regionCode.PadLeft(regionCode.Length + 1, '0');
        //                }
        //            }
        //            string build = dtNow.Day.ToString() + regionCode;

        //            int revision = 1;
        //            string old = String.Join(".", new string[] { oldMajor, oldMinor, oldBuild });
        //            string now = String.Join(".", new string[] { major, minor, build });
        //            if (old == now) //仅增加修订号
        //            {
        //                int.TryParse(oldRevision, out revision);
        //                revision += 1;
        //            }

        //            version = String.Join(".", new string[] { major, minor, build, revision.ToString() });
        //            AssemblyFileVersion = version;
        //        }
        //    }
            
        //    return version;
        //} 
        #endregion
    }

    public enum RegionVersionCode
    {
        信泽华 = 0,
        河南省 = 1,
        驻马店市 = 2,
        安阳市 = 3,
        焦作市 = 5,
        汝州 = 6,
        滑县 = 7,
        鹿邑 = 8,
        济源市 = 9,
        杞县 = 10,
        兰考 = 11,
        商丘 = 12
    }

}
